<template>
  <view class="or_login">
    <view class="or-img"><image :src="img" mode=""></image></view>
  </view>
</template>

<script>
import { mapMutations ,mapState} from 'vuex'
export default {
  name: 'or-login',
  computed: {
    // 以下两种方式都可以
    // ...mapState({ token: state => state.login.token })
    ...mapState('login', ['cookie'])
  },
  data() {
    return {
      img: '',
      // timestamp: new Date().getTime(),
      unikey: '',
      timer: null
    }
  },
  mounted() {
    clearInterval(this.timer)
    this.getORInfo()
    this.getORStatus()
  },
  destroyed() {
    clearInterval(this.timer)
  },
  methods: {
    ...mapMutations('login', ['updateCookie','updateORuserInfo']),
    async getORInfo() {
      const { data: res } = await uni.$http.get('login/qr/key', { timestamp: new Date().getTime() })
      // console.log(res)
      if (res.code === 200) {
        this.unikey = res.data.unikey
        // console.log(res.data)
        // 不能用post请求 .................................. weisha为啥
        const { data: res1 } = await uni.$http.get('login/qr/create', { key: this.unikey, qrimg: true, timestamp: new Date().getTime() })
        // console.log(res1)
        this.img = res1.data.qrimg
        // this.getORStatus(res.data.unikey)
      }
    },
    getORStatus() {
      this.timer = setInterval(async () => {
        const {data:res} = await uni.$http.get('login/qr/check', 
        { key: this.unikey, timestamp: new Date().getTime() })
        // console.log(res);
        if (res.code === 800) {
          clearInterval(this.timer)
           return uni.$showMsg('二维码过期')
        }
        if (res.code === 801) {
           console.log("待扫码")
        }
        if (res.code === 802) {
           console.log("确认")
        }
        if (res.code === 803) {
          clearInterval(this.timer)
           this.updateCookie(res.cookie)
           this.getUserInfo()
           return uni.$showMsg(res.message)
        }
      }, 3000)
    },
   async getUserInfo(){
      const wyf =  await uni.$http.get('user/account',{cookie:this.cookie})
      // console.log(wyf);
      const {data:wyf1} = await uni.$http.get('user/detail',{uid:wyf.data.profile.userId})
      // console.log(wyf1);
      wyf1.profile.level=wyf1.level
      this.updateORuserInfo(wyf1.profile)
      // console.log(wyf1.profile);
    }
  }
}
</script>

<style lang="scss" scoped>
.or-login {
  height: 100vh;
}
.or-img {
  margin-top: 100rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 800rpx;
}
image {
  width: 300rpx;
  height: 300rpx;
}
</style>
